<template>
	<view class="content">
		<view class="header">
			<view style="display: flex;justify-content: space-between;font-size: 16px;padding: 5px 0;">
				<text>{{userInfo.username}}</text>
				<text>{{userInfo.phone}}</text>
			</view>
			<text style="color: orange;font-size: 14px;padding: 5px 0;font-weight: bold;">*注意:<p style="text-indent: 20px;">1）每题所有项选择分数之和必须等于3</p>
				<p style="text-indent: 20px;">2）左右滑动进行上下题切换</p>
			</text>
		</view>
		<view class="warp" v-if="!is_submit">
			<swiper :duration="500">
				<swiper-item v-for="(item,index) in subjects" :key="index">
					<view class="swiper-item">
						<uni-card :title="(index + 1) + '、' + item.subjectTitle" :class="{'extra-text': item.numberTotal == 3}" :extra="(item.numberTotal == 3 ? '已填写完成' : '未填写完成')">
							<view class="subject_option">
								<view v-for="(option,optionIndex) in item.subjectOptions" :key="optionIndex">
									<text style="font-size: 16px;">{{optionLetter[optionIndex] + option.optionTitle}}</text>
									<view style="margin: 10px 0;">
										<uni-grid :column="4" :square="false" borderColor="#0faeff" @change="selectOptionFun($event,index,optionIndex)">
											<uni-grid-item v-for="(number,numberIndex) in 4" :key="numberIndex" :index="numberIndex" :class="{'option_actived' : option.optionValue == numberIndex}"
											 disabled="false">
												<view class="grid-item-box">
													<text class="text">{{numberIndex}}</text>
												</view>
											</uni-grid-item>
										</uni-grid>
									</view>
								</view>
							</view>
						</uni-card>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<view class="foot" v-if="!is_submit">
			<button type="primary" style="border-radius: 0;" :disabled="is_complete_number != subjects.length" @click="submitSubject">
				<text v-if="is_complete_number != subjects.length">已填写完成{{is_complete_number}} / {{subjects.length}}</text>
				<text v-else-if="subjects.length">已完成，提交问卷</text>
			</button>
		</view>
		<view class="submit_success" v-else>
				<img src="../static/submit_success.png" alt="" style="width: 100px;">
				<text style="padding: 10px;">提交成功</text>
				<text style="padding: 10px;color: #007AFF;" @click="checkAnalysis">点击查看报告</text>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				subjects: [],
				is_complete_number: 0,
				option_height: document.documentElement.clientHeight - 150,
				optionLetter: ['A、', 'B、', 'C、', 'D、', 'F、'],
				subjectTotalValue: {},
				userInfo: {},
				is_submit: false
			}
		},
		computed: {
			...mapState(['BASE_URL']),
		},
		onLoad() {
			let self = this;
			this.userInfo = uni.getStorageSync("userInfo");
			if (!this.userInfo.phone) {
				uni.reLaunch({
					url: './index'
				})
				return;
			}
			uni.showLoading({
				title: '请稍等...',
				mask: true
			})
			uni.request({
				url: this.BASE_URL + 'WikiCareerAssessment/getCareerAssessmentSubjects',
				method: 'POST',
				success(res) {
					uni.hideLoading()
					self.subjects = res.data.data.subjects;
				},
				fail() {
					uni.hideLoading()
				}
			})
		},
		watch: {
			subjects: {
				handler(o, n) {
					let number = 0;
					let self = this;
					let subjectTotalValue = {};
					for (let i = 0; i < n.length; i++) {
						if (n[i].numberTotal == 3) {
							number++;
						}
					}
					if (number == n.length) {
						for (let i = 0; i < n.length; i++) {
							for (let j = 0; j < n[i].subjectOptions.length; j++) {
								if (subjectTotalValue[n[i].subjectOptions[j].subType] != undefined) {
									subjectTotalValue[n[i].subjectOptions[j].subType] += Number(n[i].subjectOptions[j].optionValue);
								} else {
									subjectTotalValue[n[i].subjectOptions[j].subType] = Number(n[i].subjectOptions[j].optionValue);
								}
							}
						}
						this.subjectTotalValue = subjectTotalValue;
					}
					this.is_complete_number = number;
				},
				deep: true
			}
		},
		methods: {
			selectOptionFun(e, index, optionIndex) {
				let numberTotal = 0;
				let subject = this.subjects[index];
				for (let i = 0; i < subject.subjectOptions.length; i++) {
					if (i !== optionIndex) {
						numberTotal += subject.subjectOptions[i].optionValue;
					}
				}
				numberTotal += e.detail.index;
				if (numberTotal > 3) {
					uni.showToast({
						icon: 'none',
						title: "该题总分数不能超过3"
					})
					return;
				}
				subject.numberTotal = numberTotal;
				subject.subjectOptions[optionIndex].optionValue = e.detail.index;
			},
			
			/**
			 * 问卷数据提交
			 */
			submitSubject() {
				let self = this;
				let subjects = [];
				for (let i = 0; i < self.subjects.length; i++) {
					self.subjects[i].subjectOptions.forEach(item => {
						subjects.push(item);
					})
				}
				let data = {
					userInfo: {
						username: self.userInfo.username,
						phone: self.userInfo.phone
					},
					subjectTotalValue: self.subjectTotalValue,
					subjects: subjects
				}
				uni.showLoading({
					title: '请稍等...',
					mask: true
				})
				uni.request({
					url: this.BASE_URL + 'WikiCareerAssessment/setCareerAssessmentSubjects',
					method: 'POST',
					data: data,
					success(res) {
						uni.hideLoading();
						if (res.data.code == 200) {
							self.is_submit = true;
						} else {
							let error_msg = res.data.msg ? res.data.msg : '程序错误，请重试';
							uni.showToast({
								icon: 'none',
								title: error_msg
							})
						}
					},
					fail() {
						uni.hideLoading();
					}
				})
			},
			
			/**
			 * 查看报告
			 */
			checkAnalysis(){
				uni.reLaunch({
					url: './analysis_report'
				})
			}
		}
	}
</script>

<style>
	.content {
		box-sizing: border-box;
		padding: 10px;
		height: 100%;
	}

	.header {
		height: 100px;
		display: flex;
		flex-direction: column;
		background-color: rgba(255, 255, 255, 0.8);
	}

	.warp {
		height: calc(100% - 140px);
		overflow: auto;
	}

	swiper {
		height: 100%
	}

	.swiper-item .uni-card {
		background-color: rgba(255, 255, 255, 0.8);
	}

	.swiper-item /deep/ .uni-card__header-title-text {
		font-weight: bold;
	}

	.swiper-item /deep/ .uni-card__header-extra-text {
		color: red;
	}

	.extra-text /deep/ .uni-card__header-extra-text {
		color: #4CD964;
	}

	.subject_option {
		display: flex;
		flex-direction: column;
	}


	.grid-item-box {
		flex: 1;
		/* position: relative;
	*/
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 5px 0;
	}

	.option_actived {
		background-color: #0faeff;
		color: #FFFFFF;
	}

	.foot {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
	}
	
	.submit_success{
		height: calc(100% - 140px);
		background-color: rgba(255, 255, 255, 0.8);
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}
</style>
